<%@ page contentType="text/html;charset=UTF-8" language="java" %>  
    <%@ page import="java.sql.*" %>
<!DOCTYPE html>  
<html lang="zh-CN">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>前台主页</title>  
 
    <link rel="stylesheet" href="styles/main.css">  
 
</head>  
<style>

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #f2f5f4;
    color: #333;
}


.nav-links {
    list-style-type: none;
    padding: 0;
    margin: 0;
    display: flex;
    justify-content: center;
    background-color: #809089;
}

.nav-links li {
    padding: 10px 20px;
}

.nav-links a {
    text-decoration: none;
    color: white;
    transition: color 0.3s;
}

.nav-links a:hover {
    color: #f2f5f4;
}


header {
    padding: 20px 0;
    background-color: #809089;
    color: #fff;
    text-align: center;
    position: relative;
}

header h1 {
    margin: 0;
    padding: 10px 0;
    font-weight: bold;
    text-transform: uppercase;
}

main {
    margin: 20px;
    padding: 30px;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

footer {
    background-color: #809089;
    color: #fff;
    text-align: center;
    padding: 10px 0;
    position: fixed;
    bottom: 0;
    width: 100%;
}


@media (max-width: 768px) {
    .nav-links {
        flex-direction: column;
    }



</style>

<body>  
<header>  
    <h1>前台管理</h1>  


  
</header>  

<nav>
    <ul class="nav-links">
        <li><a href="javascript:void(0);" class="nav-link" data-target="#ingredients">点餐情况</a></li>
        <li><a href="javascript:void(0);" class="nav-link" data-target="#completion">结算情况与菜品制作状态</a></li>
         <li><a href="javascript:void(0);" class="nav-link" data-target="#cancellations">营业额</a></li>
    </ul>
</nav>

<!-- 内容区域 -->
<main>
    <section id="ingredients" class="section" style="display:none;">
    <%
    // 数据库连接信息
    String url = "jdbc:mariadb://10.220.140.102:3366/studb11";
    String user = "stua";
    String password = "zHZUA65r";
    

    // 加载并注册JDBC驱动
    Class.forName("org.mariadb.jdbc.Driver");

    try (Connection conn = DriverManager.getConnection(url, user, password)) {
    	 Class.forName("org.mariadb.jdbc.Driver");  
    	 String restaurant_id = (String) session.getAttribute("restaurant_id"); 
       String sql = "SELECT table_id, name, amount " +
                     "FROM shoppingcar WHERE restaurant_id="+restaurant_id;
        
       try (PreparedStatement stmt = conn.prepareStatement(sql)) {
           ResultSet rs = stmt.executeQuery();
           out.println("<h2>每桌点餐情况</h2>");
           out.println("<table border='1' width=500px>");
           out.println("<tr><th>桌号</th><th>菜名</th><th>数量</th></tr>");
           while (rs.next()) {
               String table_id = rs.getString("table_id");
               String name = rs.getString("name");
               String amount = rs.getString("amount");
               out.println("<tr><td>" + table_id + "</td><td>" + name + "</td><td>" + amount + "</td></tr>");
           }
           out.println("</table>");
       }
    } catch (SQLException e) {
        e.printStackTrace();
    }
%>

    </section>
    <section id="completion" class="section" style="display:none;">
    <div class="nav">

        <h2>结算详情</h2>
    </header>

</div>

<div class="container">
    <table width="80%" border="1">
       
  
      <%  

 String restaurant_id = (String) session.getAttribute("restaurant_id"); 
 javax.sql.rowset.WebRowSet webRowSet = aa.db.DBTools.getRS("SELECT * FROM shoppingcar WHERE restaurant_id="+restaurant_id);
 double totalPrice = 0;
 String currentTableId = null; 


String tableStyle = "style='margin-bottom: 20px;'"; 

if (webRowSet != null && webRowSet.size() > 0) {
    webRowSet.beforeFirst(); 
    while (webRowSet.next()) {
      

        String tableId = webRowSet.getString("table_id");

        if (currentTableId == null || !currentTableId.equals(tableId)) {
           
            if (currentTableId != null) {
                %></table><%
              
                %><div style="height: 20px;"></div><%
            }

           
            currentTableId = tableId;
            %><table width="80%" border="1" <%=tableStyle%>>
                <tr>
                    <th>桌号</th>
                    <th>菜品名称</th>
                    <th>制作状态</th>
                    <th>支付状态</th>
                </tr><%
        }

       
        %><tr onclick="fillform(this)" style="cursor:pointer">
            <td><%=webRowSet.getString("table_id")%></td>
            <td><%=webRowSet.getString("name")%></td>
            <td><%=webRowSet.getString("status")%></td>
            <td><%=webRowSet.getString("pay")%></td>
        </tr><%
    }

  
    %></table><%
   
    // %><div style="height: 20px;"></div><%

 }
 %>
    </table>
    </section>
    <section id="cancellations" class="section" style="display:none;">
         
<%   
try (Connection conn = DriverManager.getConnection(url, user, password)) {    
    // 显示所有已完成的订单    
    String sql = "SELECT * FROM shoppingcar WHERE pay = '已支付' AND restaurant_id="+restaurant_id;    
    try (PreparedStatement stmt = conn.prepareStatement(sql)) {    
        stmt.setString(1, restaurant_id); // 设置参数值  
        try (ResultSet rs2 = stmt.executeQuery()) {    
            out.println("<h2>已完成订单</h2>");    
            out.println("<table border='1' width='500px'>");    
            out.println("<tr><th>桌号</th><th>已支付金额</th></tr>");    
            double orderPrice = 0; // 初始化总价为0  
            double turnover = 0; // 初始化营业额为0  
  
            while (rs2.next()) {    
                String table_id = rs2.getString("table_id");    
                double price = Double.parseDouble(rs2.getString("price")); // 假设price是数据库的double类型  
                int amount = Integer.parseInt(rs2.getString("amount")); // 假设amount是数据库的int类型  
                orderPrice = price * amount; // 计算当前订单的总价  
                totalPrice += orderPrice; // 累加当前订单的价格到总价  
                  
                out.println("<tr><td>" + table_id + "</td><td>" + orderPrice + "</td></tr>");  // 显示当前订单的总价  
                turnover += orderPrice; // 累加当前订单的价格到营业额  
            }    
  
            // 输出总价格和总营业额    
            out.println("</table><br>");    
            out.println("<h3>总订单金额: " + totalPrice + "</h3>");    
            out.println("<h3>总营业额: " + turnover + "</h3>"); // 注意这里总营业额和总订单金额是一样的，除非有其它业务逻辑  
  
            // 按时间范围统计营业额的逻辑将更复杂，可能需要额外的SQL查询或Java逻辑    
            // ...    
  
        } catch (SQLException e) {    
            e.printStackTrace();    
        }    
    } catch (SQLException e) {    
        e.printStackTrace();    
    }    
} catch (SQLException e) {    
    e.printStackTrace();    
}    
%>
    </section>
</main>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        const navLinks = document.querySelectorAll('.nav-link');

        navLinks.forEach(link => {
            link.addEventListener('click', function () {
           
                document.querySelectorAll('.section').forEach(section => {
                    section.style.display = 'none';
                });

              
                const target = document.querySelector(this.getAttribute('data-target'));
                if (target) {
                    target.style.display = 'block';
                }
            });
        });
    });
</script>
<footer>  


    <p>前台管理系统</p>  
</footer>  

  
</body>  
</html>